import {
  getJieCheInitInfo,
  getMerchantInfo,
  getBossQcodeInfo,
  reportCreate,
  sendNotice,
  getCarBaoxianInfo
} from '../../../utils/networkRequest.js'

import {
  errorMsg
} from '../../../utils/tool.js'

Page({
  data: {
    tabList: [{
      id: 1,
      iconSrc: "../../../assets/images/kaidan.png",
      title: "项目开单",
      isActive: true,
    }, {
      id: 2,
      iconSrc: "../../../assets/images/jianche.png",
      title: "车况检测",
      isActive: false,
    }, {
      id: 3,
      iconSrc: "../../../assets/images/xiaofeijilu.png",
      title: "消费记录",
      isActive: false,
    }, {
      id: 4,
      iconSrc: "../../../assets/images/vip.png",
      title: "会员套餐",
      isActive: false,
    }],
    currentTabId: 1, //当前选中的id

    tatlInfo: null, //接车界面初始化信息

    checkedValue: "0", //检测类型
    activeColor: '#3274f6',
    dataInfo: null,
    mileage: "", //里程数
    qcode: null,
    orderInfo: null, //订单信息
    shoppingCartArr: [], //购物车

  },
  onLoad: function(options) {
    let qcode =options.qc;
    let self = this;
    self.setData({
      qcode: qcode
    })
    // getJieCheInitInfo(qcode, (r) => {
    //   if (r.code == 0) {
    //     self.setData({
    //       ...r.data
    //     })
    //     console.log(r)
    //   } else {
    //     errorMsg(r.message, 3);
    //   }

    // })

    // if (qcode) {
    //   getBossQcodeInfo(qcode, (r) => {
    //     if (r.code == 0) {
    //       self.setData({
    //         dataInfo: r.data,
    //         mileage: r.data.auto.mileage ? r.data.auto.mileage : "",
    //         qcode: qcode
    //       })
    //     } else {
    //       errorMsg(r.message, 3)
    //     }
    //   })
    // }
  },
  onShow:function(){

    let qcode = this.data.qcode;
    let self = this;
    getJieCheInitInfo(qcode, (r) => { 
      if (r.code == 0) {
        self.setData({
          ...r.data
        })
        console.log(r)
      } else {
        errorMsg(r.message, 3);
      }

    })

    if (qcode) {
      getBossQcodeInfo(qcode, (r) => {
        if (r.code == 0) {
          self.setData({
            dataInfo: r.data,
            mileage: r.data.auto.mileage ? r.data.auto.mileage : "",
            qcode: qcode
          })
        } else {
          errorMsg(r.message, 3)
        }
      })
    }
  },
  //新增办理
  goHandle: function() {  
    wx.navigateTo({
      url: '../handlelist/handlelist?qc=' + this.data.qcode + '&has_qcode=' + this.data.auto_info.has_qcode,
    })
  },
  //顶部tab切换
  tabChange: function(e) {
    let curid = e.currentTarget.dataset.curid;
    this.data.tabList.map((o, i) => {
      if (o.id == curid) {
        o.isActive = true;
      } else {
        o.isActive = false;
      }
    })

    this.setData({
      tabList: this.data.tabList,
      currentTabId: curid
    })
  },
  calluser: function() {
    let phone = this.data.auto_info && this.data.auto_info.phone
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: phone //仅为示例，并非真实的电话号码
      })
    } else {
      wx.showToast({
        icon: "none",
        title: '该车主没有留下手机号～',
      })
    }

  },
  //开单－添加项目
  selectPro: function() {
    let token  = null;
    try {
      token = wx.getStorageSync('token')
      
      if (value) {
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }

    debugger;
    if (this.data.orderInfo) {
      wx.navigateTo({
        url: '../selectPro/selectPro?isKaiDan=true&isEdit=true&qcode=' + this.data.qcode + '&shoppingCartArr=' + JSON.stringify(this.data.orderInfo)+'&user_id='+token.id
      })
    } else {
      wx.navigateTo({
        url: '../selectPro/selectPro?isKaiDan=true&qcode=' + this.data.qcode + '&user_id=' + token.id
      })
    }


  },
  //检测－获取检测类型
  handleSelectChange({
    detail
  }) {
    this.setData({
      checkedValue: detail.value
    })
  },
  //检测－获取里程数
  getmileage: function(e) {
    this.setData({
      mileage: e.detail.detail.value
    })
  },
  //检测－开始检测
  jianCheBegin: function() {
    let {
      qcode,
      mileage,
      checkedValue
    } = this.data;

    console.log(mileage);
    reportCreate(qcode, mileage, checkedValue, ((r) => {
      if (r.code == 0) {
        //报告生成成功
        wx.navigateTo({
          url: '../../checkitem/checkitem?qc=' + qcode + '&report_id=' + r.data.report_id
        })
      }
    }))
  },
  //发送账单给车主
  sendNotice: function() {
    let {
      orderInfo
    } = this.data;
    if (orderInfo) {
      wx.showModal({
        title: '提示',
        content: '确定要向客户推送订单吗？',
        success: function(res) {
          if (res.confirm) {
            sendNotice(orderInfo.id, (r) => {
              if (r.code == 0) {
                wx.showToast({
                  icon: "none",
                  title: '发送成功',
                })
              }
            })
          } else if (res.cancel) {
            //点击取消
          }
        }
      })
    } else {
      wx.showToast({
        icon: "none",
        title: '请先添加项目',
      })
    }

  },
  //跳转至消费详情
  goOrderDetail: function(e) {
    let id = e.currentTarget.dataset.id
    debugger;
    wx.navigateTo({
      url: "../../jiechedanbox/orderDetail/orderDetail?id=" + id
    })
  },
  //查看套餐剩余次数
  goDetail: function(e) {
    wx.navigateTo({
      url: '../handleconfirm/handleconfirm?tagindex=0&item=' + JSON.stringify(e.currentTarget.dataset.item) + "&qc=" + this.data.qc+"&read=true",
    })
  },
  //查看详情
  gobaoxianDetail: function (e) {
    let carNumber = e.currentTarget.dataset.carnumber;
    getCarBaoxianInfo(carNumber, (r) => {
      if (r.code == 0) {
        console.log(r)
        wx.navigateTo({
          url: '../../baoxianbox/baoxiandetail/baoxiandetail?dataInfo=' + JSON.stringify(r.data),
        })
      } else {
        wx.showToast({
          icon: "none",
          title: r.message,
        })
      }
    })
  },

})